package com.feige.weibo.home;

import android.content.Context;
import android.util.Log;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.feige.weibo.apiservice.AccessTokenKeeper;
import com.feige.weibo.apiservice.ApiServer;
import com.feige.weibo.apiservice.HttpUtil;
import com.feige.weibo.apiservice.URL;
import com.feige.weibo.beans.Status;
import com.feige.weibo.gloable.AppSettings;
import com.feige.weibo.gloable.MyApplication;
import com.feige.weibo.util.HttpRequest;

import org.json.JSONObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2016/10/18.
 */

public class HomeFragmentPresenter implements HomeFragmentContract.Presenter  {
    private Context mContext;
    private List<Status> weiboList;
    private HomeFragmentContract.View  view;
    private static final String TAG = "HomeFragmentPresenter";

    public HomeFragmentPresenter(Context mContext,HomeFragmentContract.View view) {
        this.mContext = mContext;
        this.view = view;

        view.setPresenter(this);
    }

    @Override
    public void loadingWeiBo(int page) {


       ApiServer apiServer = HttpUtil.getSingletonInstance();
        Map<String,String> map = new HashMap<>();
        map.put(URL.TOKEN_KEY,AccessTokenKeeper.readAccessToken(mContext).getToken());
        //map.put("page",""+AppSettings.FIRST_PAGE);


       apiServer.getWeibo(URL.URL_home_timeline,map)
               .subscribeOn(Schedulers.io())
               .observeOn(AndroidSchedulers.mainThread())
               .subscribe(new Subscriber<List<Status>>() {
                   @Override
                   public void onCompleted() {
                       view.hideProgress();

                   }

                   @Override
                   public void onError(Throwable e) {
                       view.loadFailed(e.getCause()+"   "+e.getMessage());
                       view.hideProgress();
                       Log.d(TAG, "onError: "+e.getMessage()+e.getStackTrace());
                   }

                   @Override
                   public void onNext(List<Status> status) {
                       Log.d(TAG, "onNext: "+status.get(0).getText());

                       view.loadCompleted(status);


                   }
               });

    }

    @Override
    public void start() {

    }
}
